package com.shop.front.controller.user;

import com.shop.domain.user.UserAddress;
import com.shop.front.request.user.UserAddressReq;
import com.shop.front.service.user.UserAddressService;
import com.shop.front.shiro.UserTokenUtil;
import com.shop.web.context.WebContext;
import com.shop.web.response.BaseResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(description = "用户收货地址相关", position = -1)
@RestController
@RequestMapping(value = "/userAddress", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@Slf4j
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    @ApiOperation(value = "我的默认收货地址")
    @ApiResponses(@ApiResponse(code = 10000, response = UserAddress.class, message = "content"))
    @RequestMapping("/findDefault")
    public BaseResponse findDefault() {
        Long userId = UserTokenUtil.getUser().getId();
        return userAddressService.findDefault(userId);
    }

    @ApiOperation(value = "我的收货地址")
    @ApiResponses(@ApiResponse(code = 10000, response = UserAddress.class, message = "content"))
    @RequestMapping("/page")
    public BaseResponse page() {
        Long userId = UserTokenUtil.getUser().getId();
        return userAddressService.page(userId);
    }

    @ApiOperation(value = "添加收货地址")
    @ApiResponses(@ApiResponse(code = 10000, response = UserAddress.class, message = "content"))
    @RequestMapping("/add")
    public BaseResponse add(@RequestBody @Validated UserAddressReq userAddressReq) {
        Long userId = UserTokenUtil.getUser().getId();
        return userAddressService.add(userId, userAddressReq);
    }

    @ApiOperation(value = "获取收货地址")
    @ApiResponses(@ApiResponse(code = 10000, response = String.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="sign", required = true, value = "id", paramType = "body"))
    @RequestMapping("/findSingle")
    public BaseResponse findSingle() {
        Long id = WebContext.getSignId();
        if(null == id) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }

        return userAddressService.findSingle(id);
    }

    @ApiOperation(value = "修改收货地址")
    @ApiResponses(@ApiResponse(code = 10000, response = UserAddress.class, message = "content"))
    @RequestMapping("/edit")
    public BaseResponse edit(@RequestBody @Validated UserAddressReq userAddressReq) {
        Long id = WebContext.getSignId();
        if(null == id) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }

        return userAddressService.edit(id, userAddressReq);
    }

    @ApiOperation(value = "删除收货地址")
    @ApiResponses(@ApiResponse(code = 10000, response = String.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="sign", required = true, value = "id", paramType = "body"))
    @RequestMapping("/del")
    public BaseResponse del() {
        Long id = WebContext.getSignId();
        if(null == id) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }

        return userAddressService.del(id);
    }


}
